package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.BmsUser;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiUser.GetIdentifyingCodeDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.LoginDxshAppUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.RegisterDxshAppUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.apiUser.GetMidongdongUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.apiUser.GetOneTimeTokenDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessAuthRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserSmsService;
import com.syni.mdd.sjb.common.datatreaure.service.UserService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.apiUser.LoginDxshAppUserRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.user.DxshAppUserInfoRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.BaseRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.user.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cfx
 * @since 2020-04-20
 */
@Service
@Log4j2
public class UserServiceImpl implements UserService {

    @Autowired
    UserRestService userRestService;

    @Autowired
    DxshAppUserSmsService dxshAppUserSmsService;

    @Autowired
    BusinessAuthRestService businessAuthRestService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    private final static Map<Integer,String> userAccountIdMap = new HashMap<>();

    private final static Map<String,Integer> accountUserIdMap = new HashMap<>();


    @Override
    public Map<String, Object> register(RegisterVo registerVo) {
        RpcResponseVo rpcResponseVo = userRestService.registerDxshAppUser(registerVo.getPhone(),registerVo.getVerificationCode(),registerVo.getPassword());
        if(rpcResponseVo.isSuccess()){
//            RegisterBo registerBo = rpcResponseVo.parseObject(RegisterBo.class);
//            RegisterRpVo registerRpVo = new RegisterRpVo();
//            BeanUtils.copyProperties(registerBo,registerRpVo);
            RegisterDxshAppUserDto registerDxshAppUserDto = rpcResponseVo.parseObject(RegisterDxshAppUserDto.class);

            //初始化用户短信
            dxshAppUserSmsService.intoUserSms(registerDxshAppUserDto.getDxshAppUserInfo().getUserId());

            return ResultMapHelper.success(registerDxshAppUserDto,"注册成功");
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> register_v2(RegisterVo registerVo) {
        RpcResponseVo rpcResponseVo = userRestService.registerDxshAppUser(registerVo.getPhone(),registerVo.getVerificationCode(),registerVo.getPassword());
        if(rpcResponseVo.isSuccess()){
//            RegisterBo registerBo = rpcResponseVo.parseObject(RegisterBo.class);
//            RegisterRpVo registerRpVo = new RegisterRpVo();
//            BeanUtils.copyProperties(registerBo,registerRpVo);
            RegisterDxshAppUserDto registerDxshAppUserDto = rpcResponseVo.parseObject(RegisterDxshAppUserDto.class);

            //初始化用户短信
            dxshAppUserSmsService.intoUserSms(registerDxshAppUserDto.getDxshAppUserInfo().getUserId());

            return ResultMapHelper.success(registerDxshAppUserDto,"注册成功");
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }




    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        RpcResponseVo rpcResponseVo = userRestService.loginDxshAppUser(loginVo.getPhone(),loginVo.getLoginWay(),loginVo.getLoginBody());
        if(rpcResponseVo.isSuccess()){
//            LoginBo loginBo = rpcResponseVo.parseObject(LoginBo.class);
//            LoginRpVo loginRpVo = new LoginRpVo();
//            BeanUtils.copyProperties(loginBo,loginRpVo);
            LoginDxshAppUserDto loginDxshAppUserDto = rpcResponseVo.parseObject(LoginDxshAppUserDto.class);
            LoginDxshAppUserRpVo loginDxshAppUserRpVo = BeanUtils.copyProperties(loginDxshAppUserDto,LoginDxshAppUserRpVo.class);
            //查询是否拥有店铺
            loginDxshAppUserRpVo.setDxshAppUserInfo(BeanUtils.copyProperties(loginDxshAppUserDto.getDxshAppUserInfo(),LoginDxshAppUserRpVo.DxshAppUserInfo.class));
            loginDxshAppUserRpVo.setUser(BeanUtils.copyProperties(loginDxshAppUserDto.getUser(),LoginDxshAppUserRpVo.User.class));
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq(DxshAppUserChannel.USER_ID,loginDxshAppUserRpVo.getDxshAppUserInfo().getUserId());
            int count = dxshAppUserChannelService.count(queryWrapper);
            if(count > 0){
                loginDxshAppUserRpVo.getUser().setIsHavingBusiness(Constants.COMMON_FLAG_TRUE_INT);
            }
            //初始化用户短信
            dxshAppUserSmsService.intoUserSms(loginDxshAppUserDto.getDxshAppUserInfo().getUserId());
            return ResultMapHelper.success(loginDxshAppUserRpVo,"登陆成功");
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> forget(ForgetVo forgetVo) {
        RpcResponseVo rpcResponseVo = userRestService.forgetDxshAppUser(forgetVo.getPhone(),forgetVo.getIdentifyCode(),forgetVo.getPassword());
        if(rpcResponseVo.isSuccess()){
            return ResultMapHelper.success(rpcResponseVo.getMsg());
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> getIdentifyingCode(GetIdentifyingCodeDto getIdentifyingCodeVo) {
        RpcResponseVo rpcResponseVo = userRestService.getIdentifyingCode(getIdentifyingCodeVo);
        if(rpcResponseVo.isSuccess()){
            return ResultMapHelper.success(rpcResponseVo.getData(),rpcResponseVo.getMsg());
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> saveUserInfo(SaveUserInfoVo saveUserInfoVo) {
        RpcResponseVo rpcResponseVo = userRestService.saveUserInfo(saveUserInfoVo.getUserId(),
                saveUserInfoVo.getHeadImg(),saveUserInfoVo.getSurName(),saveUserInfoVo.getName(),
                saveUserInfoVo.getSex(),saveUserInfoVo.getAge());
        if(rpcResponseVo.isSuccess()){
            DxshAppUserInfoRpVo dxshAppUserInfoRpVo = rpcResponseVo.parseObject(DxshAppUserInfoRpVo.class);
            return ResultMapHelper.success(dxshAppUserInfoRpVo, ResultMsg.HAND_SUCCESS);
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> modifyPwd(ModifyPwdVo modifyPwdVo) {
        if(!StringUtils.equals(modifyPwdVo.getNewPwdA(),modifyPwdVo.getNewPwdB())){
            return ResultMapHelper.failure("新密码不一致");
        }
        RpcResponseVo rpcResponseVo = userRestService.modifyPwd(modifyPwdVo.getUserId(),modifyPwdVo.getIdentifyCode(),
                modifyPwdVo.getNewPwdA());
        if(rpcResponseVo.isSuccess()){
            return ResultMapHelper.success(rpcResponseVo.getData(),rpcResponseVo.getMsg());
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
    }

    @Override
    public Map<String, Object> getOneTimeToken(BaseRqVo baseRqVo) {
       if(baseRqVo.getUserId() == null){
           return ResultMapHelper.failure("请先登陆再试");
       }
       GetMidongdongUserDto getMidongdongUserDto = new GetMidongdongUserDto();
       getMidongdongUserDto.setUserId(baseRqVo.getUserId());
       RpcResponseVo rpcResponseVo = userRestService.getMidongdongUser(getMidongdongUserDto);
       if(rpcResponseVo.isSuccess()){
           BmsUser bmsUser = rpcResponseVo.parseObject(BmsUser.class);
           if(bmsUser.getPhone() == null){
               return ResultMapHelper.failure("请先绑定手机");
           }
           String userName = bmsUser.getPhone();
           GetOneTimeTokenDto getOneTimeTokenDto = new GetOneTimeTokenDto();
           getOneTimeTokenDto.setUserName(userName);
           RpcResponseVo rpcResponseVo2 = userRestService.getOneTimeToken(getOneTimeTokenDto);
           if(rpcResponseVo2.isSuccess()){
               GetOneTimeTokenDto getOneTimeTokenDto1 = rpcResponseVo2.parseObject(GetOneTimeTokenDto.class);
               return ResultMapHelper.success(getOneTimeTokenDto1);
           }


       }
       return ResultMapHelper.failure("获取失败");

    }

    public String getAccountIdByUserMap(Integer userId){
        log.info("getAccountIdByUserMap:{}",userId);
        if(userAccountIdMap.containsKey(userId)){
            return userAccountIdMap.get(userId);
        }
        try {
            GetMidongdongUserDto getMidongdongUserDto = new GetMidongdongUserDto();
            getMidongdongUserDto.setUserId(userId);
            RpcResponseVo rpcResponseVo = userRestService.getMidongdongUser(getMidongdongUserDto);
            if(!rpcResponseVo.isSuccess()) {
                userAccountIdMap.put(userId,"");
                return "";
            }
            getMidongdongUserDto = rpcResponseVo.parseObject(GetMidongdongUserDto.class);
            userAccountIdMap.put(userId,getMidongdongUserDto.getAccountId());
            return getMidongdongUserDto.getAccountId();
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return "";
    }

    public Integer getUserIdByAccountIdMap(String accountId){
        log.info("getUserIdByAccountIdMap:{}",accountId);
        if(accountUserIdMap.containsKey(accountId)){
            return accountUserIdMap.get(accountId);
        }
        try {
            GetMidongdongUserDto getMidongdongUserDto = new GetMidongdongUserDto();
            getMidongdongUserDto.setAccountId(accountId);
            RpcResponseVo rpcResponseVo = userRestService.getMidongdongUser(getMidongdongUserDto);
            if(!rpcResponseVo.isSuccess()) {
                accountUserIdMap.put(accountId,0);
                return 0;
            }
            getMidongdongUserDto = rpcResponseVo.parseObject(GetMidongdongUserDto.class);
            log.info("userId :{}", getMidongdongUserDto.getUserId());
            accountUserIdMap.put(accountId,getMidongdongUserDto.getUserId());
            return getMidongdongUserDto.getUserId();
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return 0;
    }
}
